package com.unohacha.qqj.fzdr.view.citypickview;

import com.unohacha.qqj.fzdr.bean.citybean.ProvinceBean;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;


public class XmlParserHandler2 extends DefaultHandler {

    /**
     */
    private List<ProvinceBean> provinceList = new ArrayList<ProvinceBean>();

    public XmlParserHandler2() {

    }

    public List<ProvinceBean> getDataList() {
        return provinceList;
    }

    @Override
    public void startDocument() throws SAXException {
    }

    ProvinceBean provinceModel = new ProvinceBean();
    ProvinceBean.TwoRegBean cityModel = new ProvinceBean.TwoRegBean();
    ProvinceBean.TwoRegBean.ThreeRegBean districtModel = new ProvinceBean.TwoRegBean.ThreeRegBean();

    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        if (qName.equals("province")) {
            provinceModel = new ProvinceBean();
            provinceModel.setTwo_reg(new ArrayList<ProvinceBean.TwoRegBean>());
        } else if (qName.equals("city")) {
            cityModel = new ProvinceBean.TwoRegBean();
            cityModel.setThree_reg(new ArrayList<ProvinceBean.TwoRegBean.ThreeRegBean>());
        } else if (qName.equals("district")) {
            districtModel = new ProvinceBean.TwoRegBean.ThreeRegBean();
            districtModel.setId(Integer.parseInt(attributes.getValue(1)));
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equals("district")) {
            cityModel.getThree_reg().add(districtModel);
        } else if (qName.equals("city")) {
            provinceModel.getTwo_reg().add(cityModel);
        } else if (qName.equals("province")) {
            provinceList.add(provinceModel);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
    }

}
